package com.cars.satoken.controller;

import cn.dev33.satoken.listener.SaTokenEventCenter;
import cn.dev33.satoken.listener.SaTokenListener;
import cn.dev33.satoken.util.SaResult;
import com.alibaba.fastjson2.JSON;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author: caoqianfan
 * @date: 2023/11/21 14:16
 */
@RestController
@RequestMapping("/eventCenter")
public class EventCenterController {
    @GetMapping("/getListenerList")
    public SaResult getListenerList() {
        List<SaTokenListener> listenerList = SaTokenEventCenter.getListenerList();
        return SaResult.ok(JSON.toJSONString(listenerList));
    }

    @PostMapping("/setListenerList")
    public SaResult setListenerList(@RequestBody List<SaTokenListener> listenerList) {
        SaTokenEventCenter.setListenerList(listenerList);
        return SaResult.ok();
    }

    @PostMapping("/registerListener")
    public SaResult registerListener(SaTokenListener listener) {
        SaTokenEventCenter.registerListener(listener);
        return SaResult.ok();
    }

    @PostMapping("/registerListenerList")
    public SaResult registerListenerList(@RequestBody List<SaTokenListener> listenerList) {
        SaTokenEventCenter.registerListenerList(listenerList);
        return SaResult.ok();
    }

    @DeleteMapping("/removeListener")
    public SaResult removeListener(SaTokenListener listener) {
        SaTokenEventCenter.removeListener(listener);
        return SaResult.ok();
    }

    @DeleteMapping("/clearListener")
    public SaResult clearListener() {
        SaTokenEventCenter.clearListener();
        return SaResult.ok();
    }

    @GetMapping("/hasListener")
    public SaResult hasListener(SaTokenListener listener) {
        String message = String.valueOf(SaTokenEventCenter.hasListener(listener));
        return SaResult.ok(message);
    }
}